package com.mrobuy.jdvc.dao.impl;

import com.gavel.database.SQLExecutor;
import com.mrobuy.jdvc.dao.VCProductDao;
import com.mrobuy.jdvc.entity.VCProduct;
import com.mrobuy.util.Pagination;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Repository("vcProductDao")
public class VCProductDaoImpl implements VCProductDao {

    private static Logger LOG = LoggerFactory.getLogger(VCProductDao.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<VCProduct> list() {

        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT * from VC_PRODUCT");
        try {
            List<VCProduct> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper(VCProduct.class));
            return list;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return Collections.EMPTY_LIST;
    }

    @Override
    public Pagination load(int start, int length, VCProduct condition) {
        Pagination pagination = new Pagination();

        List<Object> params = new ArrayList<>();

        StringBuilder whereCond = new StringBuilder(" where 1 =1 ");
        if ( condition.getWareId()!=null && condition.getWareId().trim().length() > 0 ) {
            whereCond.append(" and WAREID = ? ");
            params.add(condition.getWareId().trim() );
        }

        if ( condition.getName()!=null && condition.getName().trim().length() > 0 ) {
            whereCond.append("  and NAME like ? ");
            params.add("%" + condition.getName().trim() + "%");
        }

        if ( condition.getCidName1()!=null && condition.getCidName1().trim().length() > 0 ) {
            whereCond.append(" and CIDNAME1 = ? ");
            params.add(condition.getCidName1().trim() );
        }


        if ( condition.getBrandName()!=null && condition.getBrandName().trim().length() > 0 ) {
            whereCond.append(" and BRANDNAME = ? ");
            params.add(condition.getBrandName().trim() );
        }

        if ( condition.getTaskid()!=null && condition.getTaskid().trim().length() > 0 ) {
            whereCond.append(" and TASKID = ? ");
            params.add(condition.getTaskid().trim() );
        }

        try {

            int count = jdbcTemplate.queryForObject("select count(*) from VC_PRODUCT " + whereCond.toString(), params.toArray(new Object[params.size()]), Integer.class);

            params.add(start);
            params.add(length);
            List<VCProduct> list = jdbcTemplate.query("select * from VC_PRODUCT " + whereCond.toString() + " limit ?, ?", params.toArray(new Object[params.size()]), new BeanPropertyRowMapper(VCProduct.class));


            pagination.setRecordsTotal(count);
            pagination.setRecordsFiltered(count);
            pagination.setData(list);
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return pagination;
    }

    @Override
    public boolean insert(VCProduct record) {


        try {
            SQLExecutor.tempLateInsert(record, jdbcTemplate);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return  false;

//        int cnt = jdbcTemplate.update("INSERT INTO VC_PRODUCT (ID, NAME, ORIGINALCODE, ORIGINALBRAND, ORIGINALCATEGORY, ORIGINALCATEGORYDESC, TASKID) VALUES (?, ?, ?, ?, ?, ?, ?)", record.getId(), record.getName(), record.getOriginalCode(), record.getOriginalBrand(), record.getOriginalCategory(), record.getOriginalCategoryDesc(), record.getTaskid());
//
//        return (cnt==1);
    }

    @Override
    public VCProduct load(String id) {
        try {
            VCProduct task = (VCProduct)jdbcTemplate.queryForObject("select * from VC_PRODUCT where ID = ?", new Object[]{id}, new BeanPropertyRowMapper(VCProduct.class));
            return task;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return null;
    }

    @Override
    public void updateApplyId(String id, String productApplyId) {

        jdbcTemplate.update("update VC_PRODUCT set APPLYID = ? where ID = ? ", productApplyId, id);

    }

    @Override
    public void update(VCProduct product, boolean updateNull) {
        try {
            SQLExecutor.tempLateUpate(product, jdbcTemplate, updateNull);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void delete(String wareId) {
        jdbcTemplate.update("delete from VC_PRODUCT where WAREID = ?", wareId);
    }

    @Override
    public VCProduct loadWareId(String wareId) {
        try {
            VCProduct task = (VCProduct)jdbcTemplate.queryForObject("select * from VC_PRODUCT where WAREID = ?", new Object[]{wareId}, new BeanPropertyRowMapper(VCProduct.class));
            return task;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return null;
    }
}
